<template>
  <div class="ele">
    <p ref="p1">element的使用</p>
    <!-- <el-button class="btn1" type="primary">主要按钮</el-button> -->
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pwd">
        <el-input v-model="ruleForm.pwd"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即登陆</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        pwd: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        pwd: [{ required: true, message: "请填写密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      // ref是一个选择器 类似jq中的$选择器，ref不仅可以选择普通元素还可以选择组件以及循环生产的结构
      //   console.log(this.$refs.ruleForm);
      //   console.log(this.$refs.p1);
    //   这个el-form中自带的一个函数 作用是校验表单
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.ruleForm);
        } else {
          console.log("error submit!!", this.ruleForm);
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style>
.ele .btn1 {
  color: red;
}
</style>